package cn.aicnn.chatssespringboot.controller;

import cn.aicnn.chatssespringboot.common.CommonResponse;
import cn.aicnn.chatssespringboot.dto.AIAnswerDTO;
import cn.aicnn.chatssespringboot.service.Impl.GptServiceImpl;
import cn.aicnn.chatssespringboot.vo.newQuestionVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


@RestController
public class ChatController {

    //用于流式请求第三方的实现类
    @Resource
    GptServiceImpl gptService;
    @PostMapping("/api/chat")
    public Object newQuestion(@RequestBody newQuestionVO request) throws IOException, InterruptedException {
        System.out.println(request.getUsername());
        return gptService.newQuestion(request);
    }
    @GetMapping("/api/chat/stream")
    public Object newQuestionBySteam(@RequestParam("userid") String userid,@RequestParam("question") String question) throws IOException, InterruptedException{
        System.out.println("$$$$$$$"+question);
        return gptService.newQuestionByStream(userid,question);
    }

    @PostMapping("/api/chat/{chatid}")
    public Object ask(@RequestBody newQuestionVO request, @PathVariable int chatid) throws IOException, InterruptedException {
        System.out.println(request);
        return gptService.ask(request, chatid);
    }
    @GetMapping("/api/chat/stream/{chatid}")
    public Object askByStream(@PathVariable int chatid,@RequestParam("question") String question) {
        return gptService.askByStream(chatid, question);
    }

    @PostMapping("/api/chat/ask")
    public CommonResponse<String> modelUse(@RequestParam MultipartFile file) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
        StringBuilder content = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            content.append(line).append("\n");
        }
        reader.close();
        String result = content.toString();
        return CommonResponse.createForSuccess("转换成功", result);
    }
}
